package cn.net.xyan.easy.gwt.client.model;

import cn.net.xyan.easy.gwt.client.EZGwt;
import cn.net.xyan.easy.gwt.shared.StringUtils;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.shared.AutoBeanUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by zarra on 16/3/10.
 */
public class Pageable {
    static public String HEADKEY = "ez-pageable-model";
    int page;
    int pageSize;
    List<SortOrder> sortOrders = new LinkedList<>();

    public Pageable(){
        this(0,20);
    }

    public Pageable(int page,int pageSize){
        this.page = page;
        this.pageSize = pageSize;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List<SortOrder> getSortOrders() {
        return sortOrders;
    }

    public void setSortOrders(List<SortOrder> sortOrders) {
        this.sortOrders = sortOrders;
    }

    @Override
    public String toString() {
        //AutoBean<Pageable> bean = AutoBeanUtils.getAutoBean(this);
        //return AutoBeanCodex.encode(bean).getPayload();

        StringBuffer sb = new StringBuffer();

        sb.append("{");

        sb.append( StringUtils.format("\"page\":\"{0}\",\"pageSize\":\"{1}\"",page,pageSize) );

        if (getSortOrders().size()>0) {
            sb.append(",\"sortOrders\":[");
            for (int i = 0; i < getSortOrders().size(); i++) {
                if (i > 0) {
                    sb.append(",");
                }
                SortOrder sortOrder = getSortOrders().get(i);
                sb.append(sortOrder.toString());
            }
            sb.append("]");
        }

        sb.append("}");

        return sb.toString();
    }
}
